
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { regionArea,getProduct,docentCommission,batchDocentCommission,sendMsg,roleList,userDetail,cancel} from './registeredUserStatisticsServ.js'
import { makeAddress } from "utils/util"

const defultState = {
    biz:{},
    loading:false,
    roleList:[],
    showModal:false,
    userInfo:{},
    professionalBroker:{}
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'getRoleList', payload:{}});
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
        }
      })
    },
  },


  effects: {
    //取消认证
    *doCancel({ payload }, { call, put, select }){
      console.log(payload)
      let { showModal,biz} = yield select(defultState => defultState[tmpModule.namespace])
      const { success,data} = yield call(cancel,payload.accountId)
      if(success){
        message.success('取消成功！',()=>{
          biz.refreshListDatas(true)
        })
        yield put({
          type: 'updateStore', payload: {
            showModal:!showModal,
            userInfo:{},
            professionalBroker:{}
          }
        })
      }
    },
    //获取用户详情
    *getUserDetail({ payload }, { call, put, select }){
      console.log(payload)
      const { success,data} = yield call(userDetail,payload.id)
      if(success){
        yield put({
          type: 'updateStore', payload: {
            userInfo:data,
            professionalBroker:data.professionalBroker
          }
        })
      }
    },
    //获取身份
    *getRoleList({ payload }, { call, put, select }){
      const { success,rows} = yield call(roleList)
      if(success){
        let roleList = []
        rows.map((item,index)=>{
          let obj={}
          // console.log(item)
          obj.value = item.roleId
          obj.label = item.name
          roleList.push(obj)
        })
        yield put({
          type: 'updateStore', payload: {
            roleList
          }
        })
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
